home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / gas_251.zip / bin_251 / bfd / lynx-core.c < prev    next >
C/C++ Source or Header  |  1994-10-11  |  6KB  |  238 lines

  1. /* BFD back end for Lynx core files
  2.    Copyright 1993 Free Software Foundation, Inc.
  3.    Written by Stu Grossman of Cygnus Support.
  4.  
  5. This file is part of BFD, the Binary File Descriptor library.
  6.  
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or
  10. (at your option) any later version.
  11.  
  12. This program is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. GNU General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with this program; if not, write to the Free Software
  19. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  20.  
  21. #include "bfd.h"
  22. #include "sysdep.h"
  23. #include "libbfd.h"
  24.  
  25. #ifdef HOST_LYNX        /* Core files only work locally for now */
  26.  
  27. /* These are stored in the bfd's tdata */
  28.  
  29. struct lynx_core_struct 
  30. {
  31.   int sig;
  32.   char cmd[PNMLEN + 1];
  33. };
  34.  
  35. #define core_hdr(bfd) ((bfd)->tdata.lynx_core_data)
  36. #define core_signal(bfd) (core_hdr(bfd)->sig)
  37. #define core_command(bfd) (core_hdr(bfd)->cmd)
  38.  
  39. /* Handle Lynx core dump file.  */
  40.  
  41. static asection *
  42. make_bfd_asection (abfd, name, flags, _raw_size, vma, filepos)
  43.      bfd *abfd;
  44.      CONST char *name;
  45.      flagword flags;
  46.      bfd_size_type _raw_size;
  47.      bfd_vma vma;
  48.      file_ptr filepos;
  49. {
  50.   asection *asect;
  51.   char *newname;
  52.  
  53.   newname = bfd_alloc (abfd, strlen (name) + 1);
  54.   if (!newname)
  55.     return NULL;
  56.  
  57.   strcpy (newname, name);
  58.  
  59.   asect = bfd_make_section (abfd, newname);
  60.   if (!asect)
  61.     return NULL;
  62.  
  63.   asect->flags = flags;
  64.   asect->_raw_size = _raw_size;
  65.   asect->vma = vma;
  66.   asect->filepos = filepos;
  67.   asect->alignment_power = 2;
  68.  
  69.   return asect;
  70. }
  71.  
  72. /* ARGSUSED */
  73. const bfd_target *
  74. lynx_core_file_p (abfd)
  75.      bfd *abfd;
  76. {
  77.   int val;
  78.   int secnum;
  79.   struct pssentry pss;
  80.   size_t tcontext_size;
  81.   core_st_t *threadp;
  82.   int pagesize;
  83.   asection *newsect;
  84.  
  85.   pagesize = getpagesize ();    /* Serious cross-target issue here...  This
  86.                    really needs to come from a system-specific
  87.                    header file.  */
  88.  
  89.   /* Get the pss entry from the core file */
  90.  
  91.   if (bfd_seek (abfd, 0, SEEK_SET) != 0)
  92.     return NULL;
  93.  
  94.   val = bfd_read ((void *)&pss, 1, sizeof pss, abfd);
  95.   if (val != sizeof pss)
  96.     {
  97.       /* Too small to be a core file */
  98.       if (bfd_get_error () != bfd_error_system_call)
  99.     bfd_set_error (bfd_error_wrong_format);
  100.       return NULL;
  101.     }
  102.  
  103.   core_hdr (abfd) = (struct lynx_core_struct *)
  104.     bfd_zalloc (abfd, sizeof (struct lynx_core_struct));
  105.  
  106.   if (!core_hdr (abfd))
  107.     {
  108.       bfd_set_error (bfd_error_no_memory);
  109.       return NULL;
  110.     }
  111.  
  112.   strncpy (core_command (abfd), pss.pname, PNMLEN + 1);
  113.  
  114.   /* Compute the size of the thread contexts */
  115.  
  116.   tcontext_size = pss.threadcnt * sizeof (core_st_t);
  117.  
  118.   /* Allocate space for the thread contexts */
  119.  
  120.   threadp = (core_st_t *)bfd_alloc (abfd, tcontext_size);
  121.   if (!threadp)
  122.     {
  123.       bfd_set_error (bfd_error_no_memory);
  124.       return NULL;
  125.     }
  126.  
  127.   /* Save thread contexts */
  128.  
  129.   if (bfd_seek (abfd, pagesize, SEEK_SET) != 0)
  130.     return NULL;
  131.  
  132.   val = bfd_read ((void *)threadp, pss.threadcnt, sizeof (core_st_t), abfd);
  133.  
  134.   if (val != tcontext_size)
  135.     {
  136.       /* Probably too small to be a core file */
  137.       if (bfd_get_error () != bfd_error_system_call)
  138.     bfd_set_error (bfd_error_wrong_format);
  139.       return NULL;
  140.     }
  141.   
  142.   core_signal (abfd) = threadp->currsig;
  143.  
  144.   newsect = make_bfd_asection (abfd, ".stack",
  145.                    SEC_ALLOC + SEC_LOAD + SEC_HAS_CONTENTS,
  146.                    pss.ssize,
  147.                    pss.slimit,
  148.                    pagesize + tcontext_size);
  149.   if (!newsect)
  150.     {
  151.       bfd_set_error (bfd_error_no_memory);
  152.       return NULL;
  153.     }
  154.  
  155.   newsect = make_bfd_asection (abfd, ".data",
  156.                    SEC_ALLOC + SEC_LOAD + SEC_HAS_CONTENTS,
  157.                    pss.data_len + pss.bss_len,
  158.                    pss.data_start,
  159.                    pagesize + tcontext_size + pss.ssize
  160. #if defined (SPARC) || defined (__SPARC__)
  161.                    /* SPARC Lynx seems to start dumping
  162.                                   the .data section at a page
  163.                                   boundary.  It's OK to check a
  164.                                   #define like SPARC here because this
  165.                                   file can only be compiled on a Lynx
  166.                                   host.  */
  167.                    + pss.data_start % pagesize
  168. #endif
  169.                    );
  170.   if (!newsect)
  171.     {
  172.       bfd_set_error (bfd_error_no_memory);
  173.       return NULL;
  174.     }
  175.  
  176. /* And, now for the .reg/XXX pseudo sections.  Each thread has it's own
  177.    .reg/XXX section, where XXX is the thread id (without leading zeros).  The
  178.    currently running thread (at the time of the core dump) also has an alias
  179.    called `.reg' (just to keep GDB happy).  Note that we use `.reg/XXX' as
  180.    opposed to `.regXXX' because GDB expects that .reg2 will be the floating-
  181.    point registers.  */
  182.  
  183.   newsect = make_bfd_asection (abfd, ".reg",
  184.                    SEC_HAS_CONTENTS,
  185.                    sizeof (core_st_t),
  186.                    0,
  187.                    pagesize);
  188.   if (!newsect)
  189.     {
  190.       bfd_set_error (bfd_error_no_memory);
  191.       return NULL;
  192.     }
  193.  
  194.   for (secnum = 0; secnum < pss.threadcnt; secnum++)
  195.     {
  196.       char secname[100];
  197.  
  198.       sprintf (secname, ".reg/%d", BUILDPID (0, threadp[secnum].tid));
  199.       newsect = make_bfd_asection (abfd, secname,
  200.                    SEC_HAS_CONTENTS,
  201.                    sizeof (core_st_t),
  202.                    0,
  203.                    pagesize + secnum * sizeof (core_st_t));
  204.       if (!newsect)
  205.     {
  206.       bfd_set_error (bfd_error_no_memory);
  207.       return NULL;
  208.     }
  209.     }
  210.  
  211.   return abfd->xvec;
  212. }
  213.  
  214. char *
  215. lynx_core_file_failing_command (abfd)
  216.      bfd *abfd;
  217. {
  218.   return core_command (abfd);
  219. }
  220.  
  221. /* ARGSUSED */
  222. int
  223. lynx_core_file_failing_signal (abfd)
  224.      bfd *abfd;
  225. {
  226.   return core_signal (abfd);
  227. }
  228.  
  229. /* ARGSUSED */
  230. boolean
  231. lynx_core_file_matches_executable_p  (core_bfd, exec_bfd)
  232.      bfd *core_bfd, *exec_bfd;
  233. {
  234.   return true;        /* FIXME, We have no way of telling at this point */
  235. }
  236.  
  237. #endif /* HOST_LYNX */
  238.